package com.eshop.modules.activity.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.activity.service.dto.StoreCouponIssueUserDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreCouponIssueUserQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.activity.service.StoreCouponIssueUserService;
import com.eshop.modules.activity.domain.StoreCouponIssueUser;
import com.eshop.modules.activity.service.mapper.StoreCouponIssueUserMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class StoreCouponIssueUserServiceImpl extends BaseServiceImpl<StoreCouponIssueUserMapper, StoreCouponIssueUser> implements StoreCouponIssueUserService
{
    private final IGenerator generator;
    
    public void addUserIssue(final Long uid, final Integer id) {
        final StoreCouponIssueUser couponIssueUser = new StoreCouponIssueUser();
        couponIssueUser.setIssueCouponId(id);
        couponIssueUser.setUid(uid);
        this.save(couponIssueUser);
    }
    
    public Map<String, Object> queryAll(final StoreCouponIssueUserQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<StoreCouponIssueUser> page = (PageInfo<StoreCouponIssueUser>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)StoreCouponIssueUserDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<StoreCouponIssueUser> queryAll(final StoreCouponIssueUserQueryCriteria criteria) {
        return (List<StoreCouponIssueUser>)(this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(StoreCouponIssueUser.class, criteria));
    }
    
    public void download(final List<StoreCouponIssueUserDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final StoreCouponIssueUserDto yxStoreCouponIssueUser : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("领取优惠券用户ID", yxStoreCouponIssueUser.getUid());
            map.put("优惠券前台领取ID", yxStoreCouponIssueUser.getIssueCouponId());
            map.put("领取时间", yxStoreCouponIssueUser.getAddTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public StoreCouponIssueUserServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
